﻿using UnityEngine;

public class PlayerMoveController : MonoBehaviour {

	private CharacterController2D cc;
	private Animator animator;

	[SerializeField]
	private float runSpeed = 40;
	private float move = 0f;
	private bool jump = false;
	private bool crouch = false;

	private Rigidbody2D rb2;

	private void Awake() {
		cc = GetComponent<CharacterController2D>();
		animator = GetComponent<Animator>();
		rb2 = GetComponent<Rigidbody2D>();
	}

	// Update is called once per frame
	void Update() {

		move = Input.GetAxisRaw("Horizontal");

		animator.SetFloat("move", Mathf.Abs(move));

		if (Input.GetButtonDown("Jump")) {
			jump = true;
			animator.SetBool("isJump", true);
		}

		if (Input.GetButtonDown("Crouch")) {
			crouch = true;
		} else if (Input.GetButtonUp("Crouch")) {
			crouch = false;
		}

	}

	private void FixedUpdate() {
			cc.Move(move * runSpeed * Time.fixedDeltaTime, crouch, jump);
			jump = false;

	}

	public void OnLanding() {
		animator.SetBool("isJump", false);
	}

	public void OnCrouching(bool wasCrouching) {
		Debug.Log(wasCrouching);
		animator.SetBool("isCrouching", wasCrouching);
	}

}
